package bit_0525_0312;

import java.util.Stack;

public class T2_0525 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String tmp : tokens) {
            if (!isOperator(tmp)) {
                stack.push(Integer.parseInt(tmp));
            } else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (tmp) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public boolean isOperator (String string) {
        return string.equals("+") || string.equals("-") || string.equals("*") || string.equals("/");
    }
}
